<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"
>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/jquery-2.1.4.js" type="text/javascript" charset="utf-8"></script>
    <script>
        function login() {
            var username = $('#name').val()
            var password = $('#userpassword').val();
            var str = {"username": username, "password": password};
            //alert(JSON.stringify(str));
            $.ajax({
                url: "/login",
                type: "POST",
                //data:"username="+ username+"&password="+password,
                data: JSON.stringify(str),
                //headers:{}
                success: function (data, status, xhr) {
                    //alert(data.code)
                    if (data.code == 200) {
                        //获取token
                        console.log(xhr.getResponseHeader("Authorization"));
                        //alert(xhr.getResponseHeader("Authorization"))
                        var Authorization = xhr.getResponseHeader("Authorization");
                        //获取用户
                        var UserName = xhr.getResponseHeader("UserName");
                        //获取权限
                        var Roles = xhr.getResponseHeader("Roles");

                        //将token存储到localStorage
                        localStorage.setItem("Authorization", Authorization);
                        localStorage.setItem("UserName", UserName);
                        localStorage.setItem("Roles", Roles);
                        //var getData = localStorage.getItem("Authorization");
                        location.href = "/index";
                    }
                }
            });
        }

    </script>
</head>
<body>
login
<!--name要和后端设置一样，登录注销不需要自己写-->
<form th:action="@{/login}" method="post">
    账号：<input type="text" name="username" id="name">
    密码：<input type="password" name="password" id="userpassword">
    <input type="checkbox" name="rember">记住我
    <input type="button" value="登录" onclick="login()">
</form>

</body>
</html>